/*
 * Copyright (C) 2011 Mette Bank, Rikke Jensen, Kenneth Brodersen, Thomas Pedersen
 * 
 * This file is part of digiPECS.
 * 
 * digiPECS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * digiPECS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with digiPECS.  If not, see <http://www.gnu.org/licenses/>.
 */

package sw6.digipecs.widget.admin;

import java.util.ArrayList;

import sw6.digipecs.R;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageBrowser extends GridView {
	
	private ArrayList<String> mSelected = new ArrayList<String>();
	
	private class ImageAdapter extends BaseAdapter {
		private LayoutInflater mLayoutInflater;
		private ArrayList<String> mImages = new ArrayList<String>();
		
		public ImageAdapter(Activity activity) {
			final Uri uri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
			final String[] fields = { MediaStore.Images.Thumbnails.DATA };
			final String select = MediaStore.Images.Thumbnails.KIND + " = " + MediaStore.Images.Thumbnails.MINI_KIND;
			final String order = MediaStore.Images.Thumbnails.DATA;
			final Cursor cursor = activity.managedQuery(uri, fields, select, null, order);
			
			final int index = cursor.getColumnIndexOrThrow(fields[0]); 
			if (cursor.moveToFirst()) {
				do {
					mImages.add(cursor.getString(index));
				} while (cursor.moveToNext());
			}
			
			mLayoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return mImages.size();
		}

		@Override
		public Object getItem(int pos) {
			return mImages.get(pos);
		}

		@Override
		public long getItemId(int pos) {
			return pos;
		}

		@Override
		public View getView(int pos, View view, ViewGroup parent) {
			if (view == null) {
				view = mLayoutInflater.inflate(R.layout.selectableimage, parent, false); 
			}
			
			final ImageView iv = (ImageView) view.findViewById(R.id.selectImage);
			final ImageView icon = (ImageView) view.findViewById(R.id.selectIcon);
			final String image = mImages.get(pos);
			
			iv.setImageURI(Uri.parse(image));
			
			if (mSelected.contains(image)) {
				icon.setVisibility(VISIBLE);
			} else {
				icon.setVisibility(INVISIBLE);
			}
			
			view.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (icon.getVisibility() == VISIBLE) {
						mSelected.remove(image);
						icon.setVisibility(INVISIBLE);
					} else {
						mSelected.add(image);
						icon.setVisibility(VISIBLE);
					}
				}
			});
			
			return view;
		}
	}

	public ImageBrowser(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public void setUp(Activity activity) {
		setAdapter(new ImageAdapter(activity));
	}
	
	public ArrayList<String> getSelected() {
		return mSelected;
	}

}
